<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/18
 * Time: 14:50
 */
namespace app\tour\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\tour\model\Order as orderModel;
class Cancel extends Common
{
    /*
     * 核销页面
     */
    public function cancel()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['order_status'] = array('eq',1);
            if (isset($pagedata['cancel_code']) && $pagedata['cancel_code'] != '') {
                $where['cancel_code'] = array('like', '%' . $pagedata['cancel_code'] . '%');

            }
            $field = [];
            $data = orderModel::orderList($field,$where,$startrow,$pagesize,false);
            $count = orderModel::orderList(array(),$where,0,0,true);
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {
            return view();
        }
    }

    /*
     * 确认核销
     */
    public function cancelPass()
    {
        if(Request::instance()->isAjax()){
            $postData = input('param.');
            //先查询一下订单是否已经被核销了
            $order = Db::name('tour_order')->field('order_status')->where('order_id',$postData['id'])->find();
            if($order){
                if($order['order_status'] !=1){
                    return 2;
                }
            }
            //修改订单状态
            $res = Db::name('tour_order')->where('order_id',$postData['id'])->setField('order_status',3);
            if($res){
                //插入到核销表
                $data = [
                    'cancel_id'=>Session::get('t_admin_id'),
                    'order_sn'=>$postData['sn'],
                    'order_id'=>$postData['id'],
                    'create_time'=>date('Y-m-d H:i:s'),
                    'type'=>1,
                    'money'=>$postData['money']

                ];
                $res = Db::name('order_cancel')->insertGetId($data);
                if($res){
                    return 0;
                }else{
                    return 1;
                }
            }else{
                return 1;
            }
        }
    }
}